package cn.com.bdqn.utils;

import cn.com.bdqn.entity.CstCustomer;
import cn.com.bdqn.entity.CstLost;
import cn.com.bdqn.service.CstCustomerService;
import cn.com.bdqn.service.CstLostService;
import cn.com.bdqn.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 使用 ApplicationListener 来初始化一些数据到 application 域中的监听器
 */
@Component
public class MyServletContextListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private CstLostService service;
    @Autowired
    private OrdersService oService;
    @Autowired
    private CstCustomerService cService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        // 先获取到 application 上下文
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        // 写一个上下文监听器  servletContextListener
        //	 , 在初始化方法里面写
        Timer t=new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try {
                    System.out.println("开始查询数据库 判断不是星期六 2点");
                    Calendar c=Calendar.getInstance(); //日历类
                    //这行不写 就是当前时间
                    //c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-9-10 9:11:11"));
                    //获得是星期几  星期天是0  1 2
                    System.out.println(c.get(c.DAY_OF_WEEK) -1);
                    //获得是几点
                    System.out.println(c.get(c.HOUR_OF_DAY));
                    //判断..是不是礼拜6 是不是2点如果是就 查数据库  ...
                    if (c.get(c.DAY_OF_WEEK) -1 == 6 && c.get(c.HOUR_OF_DAY) == 2){
                        System.out.println("调用数据库");
                        List<CstCustomer> outCustomerList = service.queryOutCustomer();
                        CstLost lost = null;
                        for(CstCustomer out : outCustomerList){
                            lost = new CstLost();
                            lost.setLstCustNo(out.getCustNo());
                            lost.setLstCustManagerId(out.getCustManagerId());
                            lost.setLstLastOrderDate(oService.queryRecentDate(out.getCustNo()));
                            lost.setLstStatus("警告");
                            //添加流失
                            service.addLost(lost);
                            //修改状态
                            cService.updateCustStatus(out.getCustNo());
                        }
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, 0, 3600*1000); //每小时判断一次
        // 获取 application 域对象，将查到的信息放到 application 域中
        ServletContext application = applicationContext.getBean(ServletContext.class);
    }
}
